﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessObject;
using EntityObject;
using SharedLibraries;
using SharedLibraries.Constants;
using ThangNM_LIB.FUNC;

namespace ThangNM
{
    public partial class Question : System.Web.UI.Page
    {
        public ThangNM.PublicInterface.Home_New mtpHome;

        #region "Properties & Event"
        public string sLinkImage
        {
            get { return (string)ViewState["sLinkImage"]; }
            set { ViewState["sLinkImage"] = value; }
        }
        public int PageNumber
        {
            get { return (int)ViewState["PageNumber"]; }
            set { ViewState["PageNumber"] = value; }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                mtpHome = Master as ThangNM.PublicInterface.Home_New;
                mtpHome.setClearMsg();
                if (!IsPostBack)
                    BindData();
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        private void BindData()
        {
            String urlSEO = FileNameByFunction.Hoi_Dap;
            if (Request.QueryString[QueryString.page] != null)
            {
                PageNumber = Convert.ToInt32(StringEx.Trim(Request.QueryString[QueryString.page]).Replace("/", ""));
                if (PageNumber != 1)
                    urlSEO = FileNameByFunction.Hoi_Dap + ConstEx.UrlEX_C._Trang + PageNumber;
            }
            else
                PageNumber = 1;
            if (!StringEx.Compare(Request.RawUrl.Substring(1), urlSEO))
            {
                Response.Status = "301 Moved Permanently";
                Response.AddHeader("Location", System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + urlSEO);
                return;
            }
            DataTable dt = (new QuestionAnswerDAO()).SelectBy_FK_iAnswerID_iStatus(0, Convert.ToInt16(Topic_iStatus_C.ReView));
            dt.Columns.Add(new DataColumn("sDateTimeCreate", Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("LinkTopicHtml", Type.GetType("System.String")));
            dt.Columns.Add(new DataColumn("sWebsite", Type.GetType("System.String")));
            QuestionAnswerExEO _QuestionAnswerExEO = new QuestionAnswerExEO();
            object temp;
            foreach (DataRow dr in dt.Rows)
            {
                dr["sDateTimeCreate"] = Convert.ToDateTime(dr["tDateTimeCreate"]).ToString(FormatEx.Date_Time.Format_DateTime_WebView);
                dr["LinkTopicHtml"] = System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + FileNameByFunction.Hoi_Dap + "/" + RewriteUrl.ConvertToUnSign(dr["sTitle"].ToString()) + "." + dr["PK_iQuestionID"].ToString() + FileNameByFunction.HTML;
                temp = JsonEx.Json2Object(StringEx.Trim(dr["sQuestionAnswerEx"].ToString()), _QuestionAnswerExEO.GetType());
                if (temp != null)
                    _QuestionAnswerExEO = (QuestionAnswerExEO)temp;
                dr["sWebsite"] = _QuestionAnswerExEO.sWebsite;
            }
            PagedDataSource pgitems = new PagedDataSource();
            System.Data.DataView dv = new System.Data.DataView(dt);
            pgitems.DataSource = dv;
            pgitems.AllowPaging = true;
            pgitems.PageSize = 10;
            pgitems.CurrentPageIndex = PageNumber - 1;
            if (pgitems.PageCount > 1)
            {
                rptPages.Visible = true;
                System.Collections.ArrayList pages = new System.Collections.ArrayList();
                for (int i = 0; i < pgitems.PageCount; i++)
                    pages.Add((i + 1).ToString());
                rptPages.DataSource = pages;
                rptPages.DataBind();
            }
            else
                rptPages.Visible = false;
            rptQA.DataSource = pgitems;
            rptQA.DataBind();

            #region "Meta Tag"//========================================================================================
            if (PageNumber == 1)
                Page.Title = StringEx.TrimEx(TitleSEO.HoiDap + System.Configuration.ConfigurationManager.AppSettings["SLOGAN"]);
            else
                Page.Title = StringEx.TrimEx(TitleSEO.HoiDap + String.Format(TitleSEO.Trang, PageNumber) + System.Configuration.ConfigurationManager.AppSettings["SLOGAN"]);
            MetaTagEO _MetaTagEO = new MetaTagEO();
            _MetaTagEO.PK_iID = 0;
            _MetaTagEO.sTitle = Page.Title;
            _MetaTagEO.sLinkImage = "";
            _MetaTagEO.sDescription = "hỏi những gì bạn muốn và chúng tôi sẽ giúp bạn. để đóng góp cho cộng đồng hãy hỏi và trả lời";
            _MetaTagEO.sTag = "hỏi đáp, trả lời" + System.Configuration.ConfigurationManager.AppSettings["KEYWORD"];
            _MetaTagEO.tDateTimeCreate = DateTime.Now;
            _MetaTagEO.tDateTimeModify = DateTime.Now;
            _MetaTagEO.sCanonical = Request.RawUrl.Substring(1);
            _MetaTagEO.sShortLink = Request.Url.ToString();
            foreach (var item in Commons.getMetaTag(_MetaTagEO))
            {
                Header.Controls.Add(item);
            }
            foreach (var item in Commons.getLinkTag(_MetaTagEO))
            {
                Header.Controls.Add(item);
            }
            #endregion//========================================================================================
        }

        protected void rptPages_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HyperLink hplPage = (HyperLink)e.Item.FindControl("hplPage");
            hplPage.NavigateUrl = System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + FileNameByFunction.Hoi_Dap + ConstEx.UrlEX_C._Trang + hplPage.Text;
            if (Convert.ToInt32(hplPage.Text) == PageNumber)
            {
                hplPage.Enabled = false;
                hplPage.BackColor = System.Drawing.Color.Orange;
            }
        }
    }
}